<template>
    <a-card :bordered="false">
        <div class="table-operator">
            <a-upload
                name="file"
                :remove="handleGwFieldKeyWordRemove"
                :beforeUpload="gwFieldKeyWordBeforeUpload"
                :showUploadList="{ showDownloadIcon: true, showRemoveIcon: true }"
                :fileList="fkFileList"
            >
                <a-button type="primary" icon="plus" style="background-color: #557e9f"> 导入国网研究领域关键词</a-button>
            </a-upload>
            <a-button
                type="primary"
                :disabled="fkFileList.length === 0"
                :loading="fkLoading"
                style="margin-top: 16px"
                @click="importGwFieldKeywordExcel"
            >
                {{ fkLoading ? '上传中...' : '开始上传' }}
            </a-button>
            <a-divider/>
            <a-button type="primary" @click="dealMongoFruitToMysql" icon="swap" :loading="fLoading" style="background-color: #557e9f">New～1～mongo成果导入 mysql
            </a-button>
            <a-divider/>
            <a-button type="primary" @click="dealMongoTalentToMysql" icon="swap" :loading="tLoading" style="background-color: #557e9f">New～2～mongo专家导入 mysql
            </a-button>
            <a-divider/>
            <a-button type="primary" @click="dealTagData" icon="swap" :loading="eLoading" style="background-color: #557e9f">New～3～设置标签 mysql
            </a-button>
            <a-divider/>
            <a-button type="primary" @click="dealMysqlToEs" icon="swap" :loading="esLoading" style="background-color: #557e9f">New～4～mysql导入 es
            </a-button>
            <a-divider/>
        </div>
    </a-card>
</template>

<script>
import {getAction, uploadAction} from '@/api/esManage'

export default {
    name: "TalentTools",
    components: {},
    data() {
        return {
            fkLoading: false,
            fkFileList: [],
            fLoading: false,
            tLoading: false,
            eLoading: false,
            esLoading: false,
        }
    },
    computed: {},
    methods: {
        handleGwFieldKeyWordRemove(file) {
            const index = this.fileList.indexOf(file);
            const newFileList = this.fileList.slice();
            newFileList.splice(index, 1);
            this.fkFileList = newFileList;
        },
        gwFieldKeyWordBeforeUpload(file) {
            if (
                file.name.substr(file.name.length - 3).toLowerCase() != 'xls' &&
                file.name.substr(file.name.length - 4).toLowerCase() != 'xlsx'
            ) {
                this.$message.warning('只能上传xls、xlsx文件!')
            } else {
                this.fkFileList = [...this.fkFileList, file];
            }
            return false;
        },
        importGwFieldKeywordExcel() {
            this.fkLoading = true;
            let formData = new FormData()
            this.fkFileList.forEach(file => {
                formData.append('file', file);
            });
            let url = "/talents/importGwFieldKeywordExcel";
            uploadAction(url, formData).then(res => {
                console.log(res);
                if (res.data.success) {
                    this.fkLoading = false;
                    this.fkFileList = [];
                    this.$message.success('文件导入成功!')
                } else {
                    this.fkLoading = false;
                    this.$message.error('文件导入失败!')
                }
            }).catch(err => {
                console.log(err)
                this.fkLoading = false;
                this.$message.error('文件导入失败!')
            })
        },
        dealMongoFruitToMysql() {
            this.fLoading = true;
            getAction("/talents/importMongoFruitDataToMysql", {}).then(res => {
                if (res.data.success) {
                    this.fLoading = false;
                    this.$message.success('处理成功!')
                } else {
                    this.fLoading = false;
                    this.$message.error('处理失败!')
                }
            }).catch(err => {
                console.log(err)
                this.fLoading = false;
                this.$message.error('处理失败!')
            })
        },
        dealMongoTalentToMysql() {
            this.tLoading = true;
            getAction("/talents/importMongoTalentDataToMysql", {}).then(res => {
                if (res.data.success) {
                    this.tLoading = false;
                    this.$message.success('处理成功!')
                } else {
                    this.tLoading = false;
                    this.$message.error('处理失败!')
                }
            }).catch(err => {
                console.log(err)
                this.tLoading = false;
                this.$message.error('处理失败!')
            })
        },
        dealTagData() {
            this.eLoading = true;
            getAction("/talents/setTagData", {}).then(res => {
                if (res.data.success) {
                    this.eLoading = false;
                    this.$message.success('设置成功!')
                } else {
                    this.eLoading = false;
                    this.$message.error('设置失败!')
                }
            }).catch(err => {
                console.log(err)
                this.eLoading = false;
                this.$message.error('设置失败!')
            })
        },
        dealMysqlToEs() {
            this.esLoading = true;
            getAction("/talents/importMysqlDataToEs", {}).then(res => {
                if (res.data.success) {
                    this.esLoading = false;
                    this.$message.success('处理成功!')
                } else {
                    this.esLoading = false;
                    this.$message.error('处理失败!')
                }
            }).catch(err => {
                console.log(err)
                this.esLoading = false;
                this.$message.error('处理失败!')
            })
        },
    },
}
</script>
<style scoped>
</style>